/*
 * Copyright (C) 2004, Velocityme Partnership
 * ContactableValueActionForm.java
 *
 * Created on 28 September 2004, 07:40
 */

package com.velocityme.www.actionforms.nodeactionforms;

import com.velocityme.entity.ContactDetail;
import com.velocityme.entity.ContactDetailType;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author  Robert
 */
public class ContactableValueActionForm extends NodeValueActionForm {
    
    private class InternalContactDetail implements Serializable {
        Long m_detailId;
        String m_typeName;
        String m_value;
    }
    
    private Map<String,InternalContactDetail> m_contactDetailMap;
    private DecimalFormat m_decimalFormat = new DecimalFormat("000");
    
    /** Creates a new instance of ContactableValueActionForm */
    public ContactableValueActionForm() {
        m_contactDetailMap = new TreeMap<String,InternalContactDetail>();
    }

    @Override
    public void reset(ActionMapping mapping, javax.servlet.http.HttpServletRequest request) {
        m_contactDetailMap.clear();
    }
    
    public Collection getContactDetails() {
        return m_contactDetailMap.keySet();
    }
    
    public void setContactDetailTypes(Collection<ContactDetailType> contactDetailTypes) {
        for (ContactDetailType contactDetailType: contactDetailTypes) {
            String key = m_decimalFormat.format(contactDetailType.getSequenceNumber());
            m_contactDetailMap.put(key, new InternalContactDetail());
            ((InternalContactDetail)m_contactDetailMap.get(key)).m_detailId = null;
            ((InternalContactDetail)m_contactDetailMap.get(key)).m_value = "";
            ((InternalContactDetail)m_contactDetailMap.get(key)).m_typeName = contactDetailType.getName();
        }
    }
    
    public Collection<ContactDetail> getContactDetails(Collection<ContactDetailType> contactDetailTypes) {
        Collection<ContactDetail> contactDetails = new ArrayList<ContactDetail>();
        Map<String,ContactDetailType> contactDetailTypeMap = new HashMap<String,ContactDetailType>();
        for (ContactDetailType contactDetailType: contactDetailTypes) {
            String key = m_decimalFormat.format(contactDetailType.getSequenceNumber());
            contactDetailTypeMap.put(key, contactDetailType);
        }
        for (String key: m_contactDetailMap.keySet()) {
            InternalContactDetail internalContactDetail = m_contactDetailMap.get(key);
            ContactDetailType contactDetailType = contactDetailTypeMap.get(key);
            ContactDetail contactDetail = new ContactDetail();
            if (internalContactDetail.m_detailId != null) {
                contactDetail.setId(internalContactDetail.m_detailId);
            }
            contactDetail.setValue(internalContactDetail.m_value);
            contactDetail.setContactDetailType(contactDetailType);
            contactDetails.add(contactDetail);
        }
        return contactDetails;
    }
    
    public Long getContactDetailId(String key) {
        return m_contactDetailMap.get(key).m_detailId;
    }
    
    public void setContactDetailId(String key, Long contactDetailId) {
        if (!m_contactDetailMap.containsKey(key))
            m_contactDetailMap.put(key, new InternalContactDetail());
        m_contactDetailMap.get(key).m_detailId = contactDetailId;
    }
    
    public String getContactDetailValue(String key) {
        return m_contactDetailMap.get(key).m_value;
    }
   
    public void setContactDetailValue(String key, String value) {
        if (!m_contactDetailMap.containsKey(key))
            m_contactDetailMap.put(key, new InternalContactDetail());
        m_contactDetailMap.get(key).m_value = value;
    }
    
    public String getContactDetailTypeName(String key) {
        return m_contactDetailMap.get(key).m_typeName;
    }
   
}
